<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Request;

class User extends Backend
{
	
	public function userlist(){
		
		// 查询用户表
		// $Userlist = model('User.User')->select();
		
		$Userlist = model('User.User')->paginate(7,false,['query' => Request::instance()->param()]);
		
		// halt($Userlist);
		
		return $this->fetch('',[
			
			'userlist' => $Userlist
			
		]);
		
	}
    
	// 删除用户
	public function deluser(){
		
		if($this->request->isAjax()){
			
			// 获取启动数据
			$arrstr = $this->request->param('str');
			
			// 字符串转数组
			$arr = explode(',',$arrstr);
			
			// 循环
			foreach($arr as $item){
				
				$result = model('User.User')->destroy(['id' => $item]);
				// $result = model('User.User')->where->(['id' => $item])->delete(false);
				
			}
			
			if($result === FALSE)
			{
				$this->error('删除失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('删除成功',url('admin/user/userlist'),null,0);
			}
			
		}
		
	}
	
	// 查看删除的用户
	public function restorelist(){
		// whereNotNull('belong')  'deletetime', 'is not null'
		$restorelist = model('User.User')->onlyTrashed()->paginate(7,false,['query' => Request::instance()->param()]);
		
		return $this->fetch('',[
			
			'restorelist' => $restorelist
			
		]);
		
	}
	
	// 恢复用户
	public function restore(){
		
		if($this->request->isAjax()){
			
			// 获取启动数据
			$arrstr = $this->request->param('str');
			
			// 字符串转数组
			$arr = explode(',',$arrstr);
			
			$data = [];
			
			// 循环
			foreach($arr as $item){
				
				$data[] = [
					'id' => $item,
					'deletetime' => null
				];
			}
			
			$result = model('User.User')->isUpdate(true)->saveAll($data);
			
			if($result === FALSE)
			{
				$this->error('恢复失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('恢复成功',url('admin/user/userlist'),null,0);
			}
			
		}
		
	}
	
	// 查看用户消费记录
	public function record(){
		
		// id
		$id = $this->request->param('userid');
		
		// $recordlist = model('User.Record')->where('userid',$id)->select();
		
		$recordlist = model('User.Record')->where('userid',$id)->order('id','desc')->paginate(7,false,['query' => Request::instance()->param()]);
		
		return $this->fetch('',[
			
			'recordlist' => $recordlist
			
		]);
		
	}
	
	// 删除用户消费记录表
	public function delrecord(){
		
		if($this->request->isAjax()){
			
			// 获取启动数据
			$arrstr = $this->request->param('str');
			
			// 字符串转数组
			$arr = explode(',',$arrstr);
			
			// 循环
			foreach($arr as $item){
				
				$result = model('User.Record')->where('id',$item)->delete();
				
			}
			
			if($result === FALSE)
			{
				$this->error('删除失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('删除成功',url('admin/user/userlist'),null,0);
			}
			
		}
		
	}
	
	// 删除单条消费记录
	public function delrecords(){
		
		if($this->request->isAjax()){
			
			// 获取参数
			$id = $this->request->param('id');
			
			$result = model('User.Record')->find($id);
			
			if(!$result){
				$this->error('没有该消费记录');
			}
				
			$result = model('User.Record')->where('id',$id)->delete();
			
			if($result){
				
				$this->success('删除成功',url('admin/user/userlist'),null,0);
				
				
			}else{
				
				$this->error('删除失败');
			}
		}
		
	}
}
